<?php
declare(strict_types=1);

namespace App\Helper;

use App\Constants\ErrorCode;

class ApiHelper
{
    /**
     * 成功返回
     * @param null|mixed $data
     * @param int|null $count
     * @return array
     */
    public static function genSuccessData($data = null, ?int $count = null): array
    {
        $re = [
            'code' => 0,
            'msg' => 'OK',
        ];
        is_null($data) || $re['data'] = $data;
        is_null($count) || $re['count'] = $count;
        return $re;
    }

    /**
     * 失败返回
     * @param string|null $msg
     * @param int|mixed $code
     * @return array
     */
    public static function genErrorData(?string $msg = null, $code = 1): array
    {
        $code = intval($code);
        $code == 0 && $code = 1;
        is_null($msg) && $msg = ErrorCode::getMessage(ErrorCode::REQUEST_ERROR);
        return [
            'code' => $code,
            'msg' => $msg,
        ];
    }
}
